var Background = (function () {
    var _this = {};
    var _jxdTab;
    var _filter = {
        urls: [
            'https://www.bjjnts.cn/*',
            'https://apif.bjjnts.cn/supervises/code',
            'https://static.zpimg.cn/master/pc/static_00010/static/portrait.b5809123.png'
        ]
    };

    _this.init = function () {
        chrome.extension.onRequest.addListener(onPostMessage);
        chrome.browserAction.onClicked.addListener(onIconClicked);
        chrome.webRequest.onBeforeRequest.addListener(interceptor, _filter, ["blocking"]);
    }

    function interceptor (details) {
        if (details.initiator == 'https://www.bjjnts.cn') {
            var u = getURLRelativePath(details.url);

            if (u == 'https://www.bjjnts.cn/study/video') {
                _jxdTab = details.tabId
                resetBadge()
            }

            if (u == 'https://apif.bjjnts.cn/supervises/code') {
                notify('V','视频已暂停，需要输入验证码!');
            }

            if (u == 'https://static.zpimg.cn/master/pc/static_00010/static/portrait.b5809123.png') {
                notify('F','视频已暂停，需要人脸识别验证!');
            }
        }
    }

    

    //接收来自 inject.js的消息
    function onPostMessage (request, sender, sendResponse) {
        notify(request.tip,request.msg)
    };

    //将msg数据以桌面通知的方式显示给用户
    function notify (tip, msg) {
        //验证码与人脸识别情况下，需要人工介入，因此角标提醒
        // if (tip == 'V' || tip == 'F') {
            chrome.browserAction.setIcon({
                path: "../images/tv_lock.png"
            });
            chrome.browserAction.setBadgeText({text: tip});
            chrome.browserAction.setBadgeBackgroundColor({ color: [25, 135, 0, 250] });

        // }else {
        //     resetBadge ()
        // }

        //弹出通知框
        chrome.notifications.create('', {
            type: 'basic',
            iconUrl:'../images/notify.png',
            title: '京训钉',
            message: msg,
        }, function (id) {
            setTimeout(function(){
                chrome.notifications.clear(id, function(){});
            }, 2000);
        });
    }

    //重置角标
    function resetBadge () {
        chrome.browserAction.setIcon({
            path: "../images/tv.png"
        });
        chrome.browserAction.setBadgeText({text:null});
        chrome.browserAction.setBadgeBackgroundColor({color:[0,0,0,0]});
    }

    //图标点击事件监听
    function onIconClicked (tab) {
        resetBadge();
        chrome.tabs.update(_jxdTab, {active: true});
    }

    // find the current tab and send a message to "inject.js" 
    _this.tell = function (message, data) {
        var data = data || {};
        
        chrome.tabs.query({
            active: true
        }, function (tab) {
            if (!tab && tab.length > 0) return;
            if (tab[0].url)
                chrome.tabs.sendMessage(tab[0].id, {
                    message: message,
                    data: data
                });
        });
    };

    function getURLRelativePath (url) {
        if(url.indexOf("?") != -1){
            return url.split("?")[0];
        } else {
            return url;
        }
    }

    return _this;
}());

window.addEventListener("load", function () {
    Background.init();
}, false);